﻿using HomagChina.Basic.CCT.Db.CCT_Db;
using HomagGroup.Base.Communication.Items;
using System;
using System.Threading;
using Utility;

namespace Proxy
{
    public class InfeedTransportHandle
    {
        private SearchInfoHandle Handle_Search;
        private ModifyInfoHandle Handle_Modify;
        private InsertInfoHandle Handle_Insert;
        private PlcHandle Handle_Plc;

        public InfeedTransportHandle()
        {
            Handle_Search = new SearchInfoHandle();
            Handle_Modify = new ModifyInfoHandle();
            Handle_Insert = new InsertInfoHandle();
            Handle_Plc = new PlcHandle();
        }

        public void Position_Transport(string Location)
        {
            Item Item_OrderState = null;
            Item Item_OrderId = null;
            Item Item_Reference = null;
            Item Item_Direction = null;

            string Section = "";
            T_TransOnlineRecords t_TransOnlineRecords = new T_TransOnlineRecords();
            View_TransOnlineRecordsDetail view_TransOnlineRecords = new View_TransOnlineRecordsDetail();
            do
            {
                try
                {
                Start:
                    if (Globle.isInfeedAreaActive && !Globle.isRunEmptyActive)//进料功能开启但RunEmpty没激活，正常走
                    {
                        Thread.Sleep(1000);
                        switch (Location)
                        {
                            case "Section_2":
                                Section = "S2";
                                break;
                            case "Section_3|Position_1":
                                Section = "S3";
                                break;
                            case "Section_3|Position_2":
                                Section = "S3";
                                break;
                            default:
                                break;
                        }

                        var Reference = Handle_Plc.Read(Item_Reference, "String").ToString();
                        if (string.IsNullOrWhiteSpace(Reference))
                        {
                            goto Start;
                        }

                        var orderState = (int)(Handle_Plc.Read(Item_OrderState, "Int"));
                        if (orderState.Equals(1000) || orderState.Equals(-1))
                        {
                            goto Start;
                        }

                        t_TransOnlineRecords.Reference = Reference;

                        view_TransOnlineRecords = Handle_Search.GetTransOnLineInfo(Reference, Section);

                        if (view_TransOnlineRecords == null)
                        {
                            goto Start;
                        }

                        switch (orderState)
                        {
                            case 0:
                                Handle_Plc.Write(Item_Direction, view_TransOnlineRecords.Direction);
                                Handle_Plc.Write(Item_OrderId, (orderState == 0 ? 1 : orderState));
                                break;
                            default:
                                Handle_Plc.Write(Item_OrderId, orderState);
                                break;
                        }

                        if (!orderState.Equals(view_TransOnlineRecords.State))//如果状态相同 不处理Db了
                        {
                            t_TransOnlineRecords.State = orderState == 0 ? 1 : orderState;
                            Handle_Modify.ModifyTransState(t_TransOnlineRecords);
                        }
                        goto Start;
                    }
                }
                catch (Exception ex)
                {
                    SerilogHandle.Logger_System.Error(ex.Message);
                }
            } while (true);
        }
    }
}